﻿<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
    CodeFile="NetworkAdapters.aspx.cs" Inherits="NetworkAdapters" Title="Network adapters" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:Content ID="Content2" ContentPlaceHolderID="content" runat="Server">
    <asp:ListView ID="lvAdapters" runat="server" DataSourceID="dsAdapters">
        <LayoutTemplate>
            <table style="width: 100%" cellpadding="4" cellspacing="0">
                <thead>
                    <tr>
                        <th>
                            Connection name
                        </th>
                        <th>
                            Adapter
                        </th>
                        <th>
                            Network connection status
                        </th>
                        <th>
                            MAC address
                        </th>
                        <th>
                            More
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <tr id="itemPlaceholder" runat="server" />
                </tbody>
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr>
                <td class="item">                
                    <%#Eval("NetworkConnectionID")%>
                </td>
                <td class="item">
                    <%#Eval("Name")%>
                </td>
                <td class="item">
                    <%#Eval("NetworkConnectionStatus")%>
                </td>
                <td class="item">
                    <%#Eval("MACAddress")%>
                </td>
                <td class="item">
                    <asp:HyperLink ID="hlMore" runat="server" NavigateUrl='<%#GetMoreUrl(Container.DataItem)%>'>More</asp:HyperLink>
                </td>
            </tr>
        </ItemTemplate>
        <AlternatingItemTemplate>
            <tr>
                <td class="item2">                
                    <%#Eval("NetworkConnectionID")%>
                </td>
                <td class="item2">
                    <%#Eval("Name")%>
                </td>
                <td class="item2">
                    <%#Eval("NetworkConnectionStatus")%>
                </td>
                <td class="item2">
                    <%#Eval("MACAddress")%>
                </td>
                <td class="item2">
                    <asp:HyperLink ID="hlMore" runat="server" NavigateUrl='<%#GetMoreUrl(Container.DataItem)%>'>More</asp:HyperLink>
                </td>
            </tr>
        </AlternatingItemTemplate>
    </asp:ListView>
    <asp:Panel ID="pnAdapter" runat="server" BackColor="LightGray">
        <table style="width: 100%; background-color: White">
            <tr>
                <td class="style1">
                    Name
                </td>
                <td>
                    <asp:Label ID="labName" runat="server" />
                </td>
            </tr>
            <tr>
                <td class="style1">
                    Type
                </td>
                <td>
                    <asp:Label ID="labType" runat="server" />
                </td>
            </tr>
            <tr>
                <td class="style1">
                    Manufacturer
                </td>
                <td>
                    <asp:Label ID="labManufacturer" runat="server" />
                </td>
            </tr>            
            <tr>
                <td class="style1">
                    Status
                </td>
                <td>
                    <asp:Label ID="labStatus" runat="server"></asp:Label>
                </td>
            </tr>            
            <tr>
                <td class="style1">
                    MAC address
                </td>
                <td>
                   <asp:Label ID="labMAC" runat="server"></asp:Label> 
                </td>
            </tr>
            <tr>
                <td class="style1">
                    IP addresses
                </td>
                <td>
                    <ol id="ip" style="padding-left:20px">
                    </ol>
                </td>
            </tr>
            <tr>
                <td class="style1">
                    DHCP Enabled
                </td>
                <td>
                    <asp:CheckBox ID="cbDHCPEnabled" runat="server" Enabled="false" />
                </td>
            </tr>            
            <tr>
                <td class="style1">
                    DHCP Server
                </td>
                <td>
                    <asp:Label ID="labDHCPServer" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td class="style1">
                    DHCP Lease Obtained
                </td>
                <td>
                    <asp:Label ID="labDHCPObtained" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td class="style1">
                    DHCP Lease Expires
                </td>
                <td>
                    <asp:Label ID="labDHCPExpires" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td class="style1">
                    Gateway
                </td>
                <td>
                    <ol id="gateways" style="padding-left:20px">
                    </ol>
                </td>
            </tr>
            <tr>
                <td class="style1">
                    DNS Servers
                </td>
                <td>
                    <ol id="dns" style="padding-left:20px">
                    </ol>
                </td>
            </tr>
        </table>
        <asp:Button ID="btnClose" runat="server" Text="Close" />
        <asp:LinqDataSource ID="dsAdapters" runat="server" 
            ContextTypeName="NetworkAdaptersWrapper" TableName="Adapters">
        </asp:LinqDataSource>
    </asp:Panel>
    <cc1:ModalPopupExtender ID="extAdapter" runat="server" PopupControlID="pnAdapter"
        TargetControlID="pnAdapter" OkControlID="btnClose" OnOkScript="HideModalPopup()">
    </cc1:ModalPopupExtender>
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">

    <script type="text/javascript">
    function more(id)
    {
        document.getElementById('ctl00_updateProgress').style.visibility='visible';
        NetworkAdapterWS.GetAdapter(id,DisplayResult);        
    }
    
    function HideModalPopup(id)
    {
         var modal = $find('<%= extAdapter.ClientID %>');
         modal.hide();
    }
    
    function DisplayResult(result)
    {                          
        var doc; 
        if (window.ActiveXObject) 
        { 
            doc=new ActiveXObject("Microsoft.XMLDOM"); 
            doc.async="false"; 
            doc.loadXML(result); 
        } 
        else 
        { 
            var parser=new DOMParser(); 
            var doc=parser.parseFromString(result,"text/xml"); 
        } 

        var tags=doc.documentElement.childNodes; 
    
        for(var i=0;i<tags.length;i++) 
        {     
            if (tags[i].nodeType==1) 
            {    
                xmlvalue="";                                             
                if(tags[i].childNodes[0]!=undefined)
                {
                    var xmlvalue=tags[i].childNodes[0].nodeValue; 
                }

                switch (tags[i].nodeName) 
                { 
                    case "name":
                        document.getElementById('<%= labName.ClientID %>').innerHTML=xmlvalue;                         
                        break;                    
                    case "type":
                        document.getElementById('<%= labType.ClientID %>').innerHTML=xmlvalue;                         
                        break;  
                    case "manufacturer":
                        document.getElementById('<%= labManufacturer.ClientID %>').innerHTML=xmlvalue;                         
                        break;
                    case "connectionStatus":                      
                        document.getElementById('<%= labStatus.ClientID %>').innerHTML=xmlvalue;
                        break;                    
                    case "mac":                      
                        document.getElementById('<%= labMAC.ClientID %>').innerHTML=xmlvalue;
                        break;
                    case "addresses":                      
                        ShowIPs(tags[i]);
                        break;                    
                    case "dhcpServer":                      
                        document.getElementById('<%= labDHCPServer.ClientID %>').innerHTML=xmlvalue;
                        break;
                    case "gateways":                    
                        ShowGateways(tags[i]);
                        break;
                    case "dnsServers":
                        ShowDns(tags[i]);
                        break;
                    case "dhcpEnabled":
                        document.getElementById('<%=cbDHCPEnabled.ClientID %>').checked=xmlvalue=="True";
                        break;
                    case "dhcpObtained":
                        document.getElementById('<%=labDHCPObtained.ClientID %>').innerHTML=xmlvalue;
                        break;
                    case "dhcpExpires":
                        document.getElementById('<%=labDHCPExpires.ClientID %>').innerHTML=xmlvalue;
                        break;
                }
            }
        }
        
            
        document.getElementById('ctl00_updateProgress').style.visibility='hidden';
        var modal = $find('<%= extAdapter.ClientID %>');
        modal.show();                        
    }
    
        
    function ShowDns(dns)
    {
        if(dns.childNodes[0]!=undefined)
        {            
            var ol=document.getElementById('dns');
            ol.innerHTML="";
        
            for(var i=0;i<dns.childNodes.length;i++)
            {
                if(dns.childNodes[i].nodeType==1)
                {                                                      
                    var server=dns.childNodes[i].childNodes[0].nodeValue;
                    var li= document.createElement("li");            
                    li.appendChild(document.createTextNode(server));
                    ol.appendChild(li);
                }
            }
        }
    }
    
    function ShowIPs(tag)
    {        
        if(tag.childNodes[0]!=undefined)
        {            
            var ol=document.getElementById('ip');
            ol.innerHTML="";
        
            for(var i=0;i<tag.childNodes.length;i++)
            {
                if(tag.childNodes[i].nodeType==1)
                {                                                      
                    var ip=tag.childNodes[i].childNodes[1].childNodes[0].nodeValue;
                    var mask=tag.childNodes[i].childNodes[3].childNodes[0].nodeValue;                                       
                    
                    var li= document.createElement("li");            
                    li.appendChild(document.createTextNode(ip+" (mask: "+mask+")"));
                    ol.appendChild(li);
                }
            }
        }
    }
    
    function ShowGateways(gateways)
    {        
        if(gateways.childNodes[0]!=undefined)
        {            
            var ol=document.getElementById('gateways');
            ol.innerHTML="";
        
            for(var i=0;i<gateways.childNodes.length;i++)
            {
                if(gateways.childNodes[i].nodeType==1)
                {                                                      
                    var server=gateways.childNodes[i].childNodes[0].nodeValue;
                    var li= document.createElement("li");            
                    li.appendChild(document.createTextNode(server));
                    ol.appendChild(li);
                }
            }
        }
    }
    </script>

    <style type="text/css">
        .style1
        {
            width: 113px;
            background-color:Gray;
        }
        td.item
        {
            background-color: #EFF3FB;
        }
        td.item2
        {
            background-color: White;
            color: #284775;
        }
        thead > tr
        {
            background-color: #5D7B9D;
            font-weight: bold;
            color: White;
        }        
    </style>
</asp:Content>
